const Service = require('egg').Service;

var uuid = require('uuid');

const crypto = require('crypto');

class UserService extends Service {
	async login(username, password) {

		const hmac = crypto.createHmac('sha256', 'djp');
		hmac.update(password);
		const password_sha256 = hmac.digest('hex');

		console.log(password_sha256);

		const res = await this.app.mysql.get('sys').query(
			' SELECT ' + 
			' u.id, u.company_code, u.username, u.name, u.mobile, c.name as company_name ' + 
			' from dlq_user as u ' + 
			' left join dlq_company as c on u.company_code = c.code' + 
			' where ' +
			' (u.username=? ' +
			' or ' +
			' u.mobile=?) ' + 
			' and ' + 
			' u.password=? and u.del_flag = "0" ', [username, username, password]
		);

		console.log(res);



		return res[0];
	}


	async list () {
		const results = await this.app.mysql.get('sys').select('dlq_user', {
			where : { del_flag : '0' },
			orders : [['create_time', 'desc']]
		});
		return results;
	}


	async update (para) {
		let result = null;

		const conn = this.app.mysql.get('sys');

		const { id } = para;

		if(id != undefined){
			result = await conn.update('dlq_user', {
				...para,
				update_time : conn.literals.now,
			});
		}else{
			result = await conn.insert('dlq_user', {
				id : uuid.v1(),
				...para,
				create_time : conn.literals.now,
				update_time : conn.literals.now,
			});
		}

		return result.affectedRows === 1;
	}

 //  	async user(userid) {
 //  		const res = await this.app.mysql.get('sys').query(
 //  			'SELECT ' + 
	// 		'a.id, ' +
	// 		'a.login_name AS "LoginName", ' +
	// 		'a.name, ' +
	// 		'a.user_type AS "userType", ' +
	// 		'a.login_ip AS "LoginIP", ' + 
	// 		//'a.login_date AS "LoginDate", ' +
	// 		'a.mobile, ' + 
	// 		'a.company_id AS "company_id", ' +
	// 		'c.name AS "company_name", ' + 
	// 		'c.code AS "company_code", ' +
	// 		'c.sys_area AS "company_sys_area", ' +
	// 		'c.binding_place as "company_binding_place" ' +
	// 		'FROM sys_user a ' +
	// 		'JOIN sys_office c ON c.id = a.company_id ' +
	// 		'WHERE a.id = ? ', [userid]
	// 	);
	// 	return res[0];
 //  	}

 //  	async auth(userid) {
 //  		const res = await this.app.mysql.get('sys').query(
 //  			'SELECT DISTINCT ' +
	// 		'a.id, ' +
	// 		'a.parent_id AS "parent.id", ' +
	// 		'a.parent_ids, ' +
	// 		'a.name, ' +
	// 		'a.href, ' +
	// 		'a.target, ' +
	// 		'a.icon, ' +
	// 		'a.sort, ' +
	// 		'a.is_show, ' +
	// 		'a.permission, ' +
	// 		'a.remarks, ' +
	// 		'a.create_by AS "createBy.id", ' +
	// 		'a.create_date, ' +
	// 		'a.update_by AS "updateBy.id", ' +
	// 		'a.update_date, ' +
	// 		'a.del_flag, ' +
	// 		'p.name AS "parent.name" ' +
	// 		'FROM sys_menu a ' +
	// 		'LEFT JOIN sys_menu p ON p.id = a.parent_id ' +
	// 		'JOIN sys_role_menu rm ON rm.menu_id = a.id ' +
	// 		'JOIN sys_role r ON r.id = rm.role_id AND r.useable="1" ' +
	// 		'JOIN sys_user_role ur ON ur.role_id = r.id ' +
	// 		'JOIN sys_user u ON u.id = ur.user_id AND u.id = ? ' +
	// 		'WHERE a.del_flag = "0" AND r.del_flag = "0" AND u.del_flag = "0"' + 
	// 		'AND a.permission is not null AND a.permission != "" ', [userid]
 //  		);

 //  		var resObj = {};

 //  		for(let i = 0, j = res.length; i < j; i++){
 //  			let tmp = res[i].permission;
 //  			let arr = tmp.split(',');
 //  			for(let m = 0, n = arr.length; m < n; m++){
 //  				let tmp1 = arr[m];
 //  				if(tmp1 != ''){
 //  					resObj[tmp1] = '1';
 //  				}
 //  			}
 //  		}

 //  		return resObj;
	// }

	// async step (company_code) {

	// 	// this.app.logger.info('step');
	// 	// this.app.logger.info(company_code);

	// 	let res = {};

	// 	try{
	// 		res = await this.app.mysql.get('weshop').query(
	// 			'SELECT step, ispossess from weshop_apply where ' +
	// 			'company_code=? ', [company_code]
	// 		);

	// 		//this.app.logger.info(res);
	// 	} catch(err) {
	// 		//this.app.logger.info(err);
	// 	}
		

	// 	if(res == null){
	// 		return {};
	// 	}else{
	// 		return res.length == 0 ? {} : res[0];
	// 	}
		
	// }


	// async getInfoByMobile (mobile) {
	// 	const res = await this.app.mysql.get('sys').query(
	// 		'SELECT id from sys_user where ' +
	// 		'mobile=? ', [mobile]
	// 	);

	// 	return res.length == 0 ? {} : res[0];
	// }


	// async modifyPass (username, newpassword, oldpassword) {

	// 	const hmac = crypto.createHmac('sha256', 'djp');
	// 	hmac.update(newpassword);
	// 	const password_sha256 = hmac.digest('hex');

	// 	const hmac1 = crypto.createHmac('sha256', 'djp');
	// 	hmac1.update(oldpassword);
	// 	const oldpassword_sha256 = hmac1.digest('hex');

	// 	const res = await this.app.mysql.get('sys').query(
	// 		'update sys_user set password=? ' + 
	// 		'where ' +
	// 		'(login_name=? ' +
	// 		'or ' +
	// 		'mobile=?) ' + 
	// 		'and ' + 
	// 		'password=? ', [password_sha256, username, username, oldpassword_sha256]
	// 	);

	// 	return res.affectedRows === 1;
	// }
}

module.exports = UserService;

